package com.uchengwang.ui.cm_ui.me.ui;

import android.os.Bundle;
import android.support.v7.widget.SwitchCompat;
import android.util.Log;
import android.widget.CompoundButton;

import com.uchengwang.R;
import com.uchengwang.functions.http.config.BaseConfig;
import com.uchengwang.functions.http.config.UserConfig;
import com.uchengwang.functions.http.entity.MapRequest;
import com.uchengwang.functions.http.listener.HttpCallBack;
import com.uchengwang.functions.http.manager.HttpManager;
import com.uchengwang.ui.base_ui.ui.BaseWithTitleActivity;
import com.uchengwang.ui.cm_ui.login.bean.UserBean;
import com.uchengwang.ui.utils.JsonUtils;
import com.uchengwang.ui.utils.LoadingUtils;
import com.uchengwang.ui.utils.UCommonUtil;


/**
 * Created by U城网 on 2017/6/12.
 * 隐私设置
 */

public class MyPrivateSetActivity extends BaseWithTitleActivity{
    //谁可见我的享说
    private SwitchCompat priSet_friend_bt;
    private boolean friendOnly ;
    private boolean isAllow = true, isInit = true;//防止多次操作
    private UserBean userBean;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_pri_set);
        setTitle("设置");
        
        initView();
    }
    
    private void initView(){
        priSet_friend_bt = (SwitchCompat) findViewById(R.id.priSet_friend_bt);//所有用户可见
        priSet_friend_bt.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (!isInit){
                    priSet_friend_bt.setChecked(isChecked);
                    postShareSet(isChecked);
                }
                isInit = false;

            }
        });

        getMyInfo();

    }
    /**
     * 查看某个人的信息
     * */
    private void getMyInfo(){
        MapRequest map = new MapRequest();
        map.put(BaseConfig.ID, UCommonUtil.getUtil().getId());

        HttpManager.newInstance(this).query(UserConfig.ChangeInfo.method_check, UserConfig.ChangeInfo.URL, map, new HttpCallBack() {
            @Override
            public void onError(int status, String msg) {
                UCommonUtil.getUtil().showToast(MyPrivateSetActivity.this, msg);
            }

            @Override
            public void onResp(int status, String msg, String data) {
                if (status == 0){
                    userBean = JsonUtils.getManager().stringToBean(data, UserBean.class);
                    initCheck(userBean);
                }else {
                    UCommonUtil.getUtil().showToast(MyPrivateSetActivity.this, msg);
                }
            }
        });

    }

    /**
     * 提交享说能见度
     * */
    private void postShareSet(boolean isFriendOnly){
        LoadingUtils.getUtils().loading(this);
        if (!isAllow){
            UCommonUtil.getUtil().showToast(this, "正在提交");
            return;
        }
        isAllow = false;
        MapRequest map =new MapRequest();
        map.put(BaseConfig.FRIEND_ONLY, isFriendOnly);

        HttpManager.newInstance(this).query(UserConfig.ChangeInfo.method_change, UserConfig.ChangeInfo.URL, map, new HttpCallBack() {
            @Override
            public void onError(int status, String msg) {
                LoadingUtils.getUtils().hide();
                Log.d("jjf", "status="+status+"=msg="+msg);
                isAllow = true;
                UCommonUtil.getUtil().showToast(MyPrivateSetActivity.this, "修改失败,请重试");
            }

            @Override
            public void onResp(int status, String msg, String data) {
                isAllow = true;
                LoadingUtils.getUtils().hide();
                Log.d("jjf", "status="+status+"=msg="+msg);
                if (status != 0){
                    UCommonUtil.getUtil().showToast(MyPrivateSetActivity.this, "修改失败,请重试");
                    return;
                }
                UCommonUtil.getUtil().showToast(MyPrivateSetActivity.this, "修改成功");
            }
        });
    }

    private void initCheck(UserBean user){
        if (user != null){
            friendOnly = user.isFriendsOnly();
        }
        if (friendOnly){
            priSet_friend_bt.setChecked(friendOnly);//这么判断是因为 当为true时, 系统会自动调用了监听方法
        }else {
            isInit = false;
        }
    }
}
